import { RecommendModalComponent } from 'src/app/coms/recommend-modal/recommend-modal.component';
import { Component, OnInit, Input } from "@angular/core";
import { AppGlobalService } from "src/app/shared/service/app-global.service";
import { HttpService } from "src/app/shared/service/http.service";
import { ActivatedRoute, Router } from "@angular/router";
import { ModalController } from '@ionic/angular';
import { NewBusinessInfoModalComponent } from '../new-business-info-modal/new-business-info-modal.component';
import { SharePageComponent } from '../share-page/share-page.component';
import { NewInquiryPageModalComponent } from '../new-inquiry-page-modal/new-inquiry-page-modal.component';
import { ModalFromCenterLeave, ModalFromCenterEnter } from 'src/app/utils/transitions';

@Component({
  selector: "app-work-center",
  templateUrl: "./work-center.component.html",
  styleUrls: ["./work-center.component.scss"],
})
export class WorkCenterComponent implements OnInit {
  constructor(
    private appGlobalService: AppGlobalService,
    private modalController: ModalController,
    private httpService: HttpService,
    private router: Router) { }
  @Input() tagData: any;
  showList = [];
  colList = [];
  shareData: any;
  ngOnInit() {
    this.showList = this.tagData.data.data;
    const len = Math.ceil(this.showList.length / 4);
    for (let i = 0; i < len; i++) {
      this.colList.push(i);
    }
  }

  share(link) {
    console.log('share --> page', link);
    // 请求参数
    const params = {
      sessionId: localStorage.getItem("sessionId"),
      pageId: link.target,
      params: link.params
    };
    this.httpService.post('application/page', params, res => {
      console.log(JSON.parse(res.script));
      this.shareData = JSON.parse(res.script).views[0].tags[0].data;
      // console.log(this.shareData);
      this.presentModal();
    });
  }

  // 分享弹框
  async presentModal() {
    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: 'sharePage',
      backdropDismiss: true,
      component: SharePageComponent,
      componentProps: { tagData: this.shareData }
    });
    await modal.present();
    // 监听销毁的事件
    const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    console.log(modalData);
  }

  handleClick(item) {
    if (item && item.type === 'modal') {
      this.openNewBusinessInfoModal("recommend-modal", item);
    } else if (item && item.type === 'share') {
      this.share(item);
    } else if (item && item.type === 'pageDialog') {
      this.openTipsPageModal("recommend-modal", item);
    } else if (item && item.type === 'confirm-modal') {
      this.openConfirmModalPageModal("recommend-modal", item);
    } else {
      const sessionId = localStorage.getItem("sessionId");
      const pageParams = {
        pageId: item.target,
        params: item.params,
        sessionId,
      };
      this.appGlobalService.goToForward(pageParams);
    }
  }

  async openTipsPageModal(className, item: any) {
    try {
      const params = {
        sessionId: localStorage.getItem("sessionId"),
        pageId: item.target,
        params: item.params
      };
      this.httpService.post('application/page', params, async res => {
        const tagData = JSON.parse(res.script).views[0].tags[0].data;
        if (tagData) {
          const modal = await this.modalController.create({
            showBackdrop: true,
            cssClass: className,
            animated: false,
            component: RecommendModalComponent,
            componentProps: { tagData },
            leaveAnimation: ModalFromCenterLeave,
            enterAnimation: ModalFromCenterEnter
          });
          await modal.present();
          const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
        }
      });
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
    }
  }

  async openConfirmModalPageModal(className, item: any) {
    try {
      const params = {
        sessionId: localStorage.getItem("sessionId"),
        pageId: item.target,
        params: item.params
      };
      this.httpService.post('application/page', params, async res => {
        const tagData = JSON.parse(res.script).views[0].tags[0].data;
        if (tagData) {
          const modal = await this.modalController.create({
            showBackdrop: true,
            cssClass: className,
            animated: false,
            component: NewInquiryPageModalComponent,
            componentProps: { tagData }
          });
          await modal.present();
          const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
        }
      });
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
    }
  }

  async openNewBusinessInfoModal(className, item: any) {
    try {
      const params = {
        sessionId: localStorage.getItem("sessionId"),
        pageId: item.target,
        params: item.params
      };
      this.httpService.post('application/page', params, async res => {
        const tagData = JSON.parse(res.script).views[0].tags[0].data;
        const modal = await this.modalController.create({
          showBackdrop: true,
          cssClass: className,
          animated: false,
          component: NewBusinessInfoModalComponent,
          componentProps: { tagData }
        });
        await modal.present();
        // 监听销毁的事件
        const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
      });
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
    }

  }
}
